package com.hugoo.core.db.rp;

import com.hugoo.rpt.ExpPageInfo;
import java.sql.ResultSet;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/**
 *
 * @author Lovelock.Chan
 */
public class XlsResultPack extends ResultPack {

    public XlsResultPack(ExpPageInfo pi, String[][] col) {
        super(pi, col);
    }

    @Override
    public void packResult(ResultSet rs) {
        WritableFont wfont; // 字体
        WritableCellFormat wcfFc; // 字体格式
        Label wlabel; // Excel表格的Cell
        WritableWorkbook ww = null;
        try {
            /////////验证列是否有效///////////////
            pi.validateColumn(rs.getMetaData());
            /////////////////////////
            ww = Workbook.createWorkbook(pi.getTempFile());// 建立excel文件
            WritableSheet ws = ww.createSheet(pi.getTitle(), 0);// sheet名称
            ////////////////////////////////////////////////////////////////////////////////////
            // 设置excel列名字体
            wfont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
            wcfFc = new WritableCellFormat(wfont);
            //创建Excel的列名
            int i = 0, r = -1, c = col[0].length;
            if (pi.isExpTitle()) {//如果需要导出标题行,执行本部分(默认是导出标题行)
                r++;
                for (String fl : col[1]) {
                    wlabel = new Label(i++, r, fl, wcfFc);
                    ws.addCell(wlabel);
                }                
            }
            // 设置excel列名字体
            wfont = new WritableFont(WritableFont.TIMES, 9, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
            wcfFc = new WritableCellFormat(wfont);
            wcfFc.setBorder(Border.ALL, BorderLineStyle.THIN);
            String fnum = pi.getFNUMColumns();
            boolean[] b = new boolean[col[0].length];
            for (int z = 0; z < c; z++) {
                b[z] = fnum.contains("," + col[0][z] + ",");
            }
            //
            Double dbv;
            while (rs.next()) {
                r++;
                i = 0;
                for (int z = 0; z < c; z++) {
                    if (b[z]) {
                        dbv = getDoubleValue(rs, col[0][z]);
                        if (dbv == null) {//如果为空，则暂时处理为不插入值
                            ws.addCell(new Label(i++, r, "", wcfFc));
                        } else {
                            ws.addCell(new jxl.write.Number(i++, r, dbv, wcfFc));
                        }
                    } else {
                        ws.addCell(new Label(i++, r, getValue(rs, col[0][z]), wcfFc));
                    }
                }
            }
            ww.write();//写入文件
        } catch (Exception ex) {
            log.warn("处理Xls数据导出时，出现异常！", ex);
        } finally {
            if (ww != null) {
                try {//关闭
                    ww.close();
                } catch (Exception ex) {
                    log.warn("关闭Xls文件时，出现异常！", ex);
                }
            }
        }
    }
}
